package com.jinhu.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.http.HttpException;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.jinhu.Entity.orderRecord;
import com.jinhu.ResultEntity;
import com.jinhu.Service.IOrderService;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.MethodType;
import org.noear.solon.core.handle.ModelAndView;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Set;

@Controller
public class OrderController {
    @Inject
    IOrderService iOrderService;

    @Mapping(value = "/getList",method = MethodType.POST)
    public ResultEntity getList(@Body JSONObject jsonObject) {
        if(JSONUtil.isNull(jsonObject)) ResultEntity.no("参数为空");
        List<orderRecord> orderByDate = iOrderService.getOrderByDate(jsonObject.getStr("userid"), jsonObject.getStr("date"));
        return ResultEntity.ok("success",orderByDate);
    }
    
    @Mapping("/addOrder")
    public ResultEntity addOrder(@Body JSONObject jsonObject) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        if(JSONUtil.isNull(jsonObject)) ResultEntity.no("参数为空");

        JSONObject body = jsonObject.getJSONObject("body");
        String userid = jsonObject.getStr("userid");
        String unique = body.getStr("name");
        JSONArray jsonArray = jsonObject.getJSONArray("obj");

        for (int i = 0;i<jsonArray.size();i++){
            orderRecord order = new orderRecord();
            String str = jsonArray.getJSONObject(i).getStr("time")
                    .replace("年","-")
                    .replace("月","-")
                    .replace("日"," ");
            LocalDateTime parse = LocalDateTime.parse(str, dateTimeFormatter);
            order.setOrderCode(jsonArray.getJSONObject(i).getStr("orderCode"));
            order.setPic(jsonArray.getJSONObject(i).getStr("pic"));
            order.setName(userid);
            order.setPrice(jsonArray.getJSONObject(i).getDouble("price"));
            order.setRemark(jsonArray.getJSONObject(i).getStr("remark"));
            order.setStatus(jsonArray.getJSONObject(i).getStr("status"));
            order.setTime(parse);
            order.setUnique(unique);
            order.setType(jsonArray.getJSONObject(i).getStr("type"));
            order.setStatus(jsonArray.getJSONObject(i).getStr("status"));
            iOrderService.insertOrder(order);
        }
        return ResultEntity.ok("插入数据成功",jsonObject);
    }

}